package com.wzq.util;

import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public  class AnalysisFile {

    public static Map<String,Object> analysisImage(String Base64Data) throws IOException {

//       String FilePath = "C:\\Users\\BarryAllen\\OneDrive\\文档\\WeChat Files\\wxid_uejbqneeutgc21\\FileStorage\\File\\2022-11\\newbase64.txt";
        //得到文件base64编码
        HashMap<String, Object> hashMap = new HashMap<>();
        String realBase64 = "";

//        System.out.println(Base64Data);

        //保存文件格式
        String fileExtname = "";
        if (Base64Data.contains("base64,") && Base64Data.startsWith("data:")) {
            //截取          data:image/jpeg;base64
            String[] typedata = Base64Data.split(",");
//            System.out.println(typedata[0]);
            //截取           jpeg;base64
            String tempType = typedata[0].split("/")[1];
//            System.out.println(tempType);
            //截取            jpeg
            fileExtname = "." + tempType.substring(0, tempType.indexOf(";"));
//            System.out.println(fileExtname);
            realBase64 = typedata[1];
        } else {
            realBase64 = Base64Data;
            fileExtname = ".jpg";
        }

        byte[] bytes = Base64.getMimeDecoder().decode(realBase64);
        hashMap.put("imagData",bytes);
        hashMap.put("imagType",fileExtname);

        return hashMap;

    }
    public static Map<String,Object> analysisVideo(String Base64Data) throws IOException {


        HashMap<String, Object> hashMap = new HashMap<>();
        String realBase64 = "";
//        System.out.println(Base64Data);
        //保存文件格式
        String fileExtname = "";
        if (Base64Data.contains("base64,") && Base64Data.startsWith("data:")) {
            //截取          data:video/mp4
            String[] typedata = Base64Data.split(",");
//            System.out.println(typedata[0]);
            //截取
            String tempType = typedata[0].split("/")[1];
//            System.out.println(tempType);
            //截取
            fileExtname = "." + tempType.substring(0, tempType.indexOf(";"));
//            System.out.println(fileExtname);
            realBase64 = typedata[1];
        } else {
            realBase64 = Base64Data;
            fileExtname = ".mp4";
        }

        byte[] bytes = Base64.getMimeDecoder().decode(realBase64);
        hashMap.put("videoData",bytes);
        hashMap.put("videoType",fileExtname);

        return hashMap;
//        File file = new File("D:/" + UUID.randomUUID().toString() + fileExtname);
//        FileOutputStream fo = new FileOutputStream(file);
//        fo.write(bytes);
//        fo.flush();
//        fo.close();
//        System.out.println("文件上传成功");
//        return "文件上传成功";
    }


}
